<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 一定要先引入vue再引入vue-router -->
    <script src="../js/vue.min.js"></script>
    <script src="../js/vue-router_3.0.2.js"></script>
</head>

<body>
    <div id="app">
        <!-- router-link是VR自带的标签，会被默认渲染为a标签 -->
        <!-- 省略了锚点前面的# -->
        <router-link to="/user">user</router-link>
        <router-link to="/register">register</router-link>
        <!-- 路由占位符，通过路由规则匹配到的组件展示的位置 -->
        <router-view></router-view>
    </div>
    <script>
        // 声明式导航：通过点击链接实现导航的方式，<a></a>之类的
        // 编程式导航：调用JS的API实现导航的方式，location.herf
        // this.$router.push('hash地址')
        // this.$router.go(n)


        // 创建子组件
        var User = {
            template: '<div><button @click="goRegister">跳转到注册页面</button></div>',
            methods: {
                goRegister: function() {
                    this.$router.push('/register');
                }
            }
        };
        const Register = {
            template: `
            <div>
                <h1>register</h1>
                <button @click="goUser">跳转到原来页面</button>
                </div>`,
            methods: {
                goUser() {
                    this.$router.go(-1)
                }
            }
        };
        // 配置路由规则!!!!!!!!!!
        const router = new VueRouter({
            routes: [{
                path: '/',
                // 路由重定向
                redirect: "/User"
            }, {
                path: "/user",
                component: User
            }, {
                path: "/register",
                component: Register

            }]
        });
        const vm = new Vue({
            el: '#app',
            // 挂载实例对象
            router: router
        })
    </script>
</body>

</html>